package manytoone.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manytoone.mapper.StudentMapper;
import manytoone.pojo.Room;
import manytoone.pojo.Student;
import manytoone.service.StudentService;
import manytoone.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/9/6
 * @Version 1.0
 **/
public class StudentServiceImpl implements StudentService {

//    private SqlSession session = MyBatisUtil.getInstance();
//    private StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    @Override
    public PageInfo<Student> findAll(String currentPage,String mohu) {
        SqlSession session= MyBatisUtil.getInstance();
        //PageHelper.startPage(,3);
        Page page = PageHelper.startPage(Integer.parseInt(currentPage),3);
        PageInfo pageInfo = new PageInfo(session.getMapper(StudentMapper.class).findAll(mohu));
        return pageInfo;
    }

    @Override
    public List<Room> findRooms() {
        SqlSession session= MyBatisUtil.getInstance();
        return session.getMapper(StudentMapper.class).findRooms();
    }

    @Override
    public boolean save(Student student) {
        SqlSession session= MyBatisUtil.getInstance();
        int m=0;
        if(student.getId()==null || student.getId()==0) {
            m = session.getMapper(StudentMapper.class).add(student);
        }else{
            m= session.getMapper(StudentMapper.class).update(student);
        }
        if(m==1) session.commit();
        return  true;

    }

    @Override
    public boolean delete(String ids) {
        System.out.println("业务层获取的删除："+ids);
        //转换为String[]
        String[] arr = ids.split(",");
        SqlSession session= MyBatisUtil.getInstance();
        int m=session.getMapper(StudentMapper.class).delete(arr);
        if(m>=1) session.commit();
        return m>=1;
    }

    @Override
    public Student findById(String sid) {
        int id=0;
        if(sid!=null){
             id=Integer.parseInt(sid);
        }
        SqlSession session= MyBatisUtil.getInstance();
        Student student = session.getMapper(StudentMapper.class).findById(id);

        return student;
    }
}
